package kodkod.ast.visitor.graph;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import kodkod.analysis.AST.GraphVisitor;
import kodkod.ast.BinaryExpression;
import kodkod.ast.BinaryFormula;
import kodkod.ast.BinaryIntExpression;
import kodkod.ast.ComparisonFormula;
import kodkod.ast.Comprehension;
import kodkod.ast.ConstantExpression;
import kodkod.ast.ConstantFormula;
import kodkod.ast.Decls;
import kodkod.ast.ExprToIntCast;
import kodkod.ast.Expression;
import kodkod.ast.Formula;
import kodkod.ast.IfExpression;
import kodkod.ast.IntComparisonFormula;
import kodkod.ast.IntConstant;
import kodkod.ast.IntExpression;
import kodkod.ast.IntToExprCast;
import kodkod.ast.LeafExpression;
import kodkod.ast.MultiplicityFormula;
import kodkod.ast.NaryExpression;
import kodkod.ast.NaryFormula;
import kodkod.ast.NaryIntExpression;
import kodkod.ast.Node;
import kodkod.ast.NotFormula;
import kodkod.ast.ProjectExpression;
import kodkod.ast.QuantifiedFormula;
import kodkod.ast.Relation;
import kodkod.ast.RelationPredicate;
import kodkod.ast.SumExpression;
import kodkod.ast.UnaryExpression;
import kodkod.ast.UnaryIntExpression;
import kodkod.ast.Variable;

public class GraphVisitorUCINET extends GraphVisitor {

	StringBuffer header = new StringBuffer();
	StringBuffer labels = new StringBuffer();
	StringBuffer edges = new StringBuffer();
	
	File graphFile = null;
	BufferedWriter writer = null;
	
	public GraphVisitorUCINET() {
		try {
			graphFile = File.createTempFile("Graph", ".dl");
			writer = new BufferedWriter(new FileWriter(graphFile));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		labels.append("labels:\n");
		edges.append("data:\n");
	}
	
	public void finalize() {
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
	public void flush() {
		header.append("DL n=" + numberOfNodes() + "\n");
		header.append("format = edgelist1\n");
		labels.append("\n");
		try {			
			writer.write(header.toString());
			writer.write(labels.toString());
			writer.write(edges.toString());
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
	@Override
	protected void newEdge(Node parent, Node child) {
		int parentId = getNodeId(parent);
		int childId = getNodeId(child);
		edges.append(parentId+" "+childId+"\n");
	}
	
	@Override
	protected void newNode(Node node) {
		if (numberOfNodes() > 1) {
			labels.append(", ");
		}
		labels.append(getNodeName(node));
	}

	@Override
	protected void enter(Node node) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void leave(Node node) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void newCompound(Node parent, Node child) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void newEdge(Node parent, Node child, int pos) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected boolean visited(Node n) {
		// TODO Auto-generated method stub
		return false;
	}
}
